import axios from "axios";
import {getCookie} from "@/utils/Cookie";
import baseInfo from '@/default/index'
import {Toast} from "vant";
import store from "@/store";
const token = getCookie('like-admin')

const api = axios.create({
    baseURL: '/api',
    withCredentials:false,
    maxRedirects:5,
    timeout:8000,
    // headers: {
    //     "Content-Type": "application/json",
    //     "Accept":"*/*",
    // }
})

// 请求拦截器

api.interceptors.request.use(
    config=>{
        if (token){
            config.headers['tk'] = token
        }else{
            config.headers['tk']=store.state.token
        }
        // config.headers['like-lang'] = baseInfo.lang

        return config
    },
    error => {
        return Promise.reject(error)
    }
)

api.interceptors.response.use(
    response =>{
        let code = response.data.code
        switch (code){
            case 20000:{
                return response.data
            }
            default:{
                Toast(response.data.msg)
                return Promise.reject(response.data)
            }
        }
    },
    error => {
        return Promise.reject(error)
    }
)

export default api